﻿using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.WebControls;

namespace TagField
{
	// TODO: Replace, as needed, "SPFieldText" with some other class derived from SPField. 
	// TODO: Update, as needed, ParentType element in fldtypes*.xml in this solution. 
	[CLSCompliant(false)]
	[Guid("5e5057f1-189c-473e-96c1-54b9ae55bdbd")]
	public class TagField : SPFieldText
	{
		#region Private Fields

		private string targetName;
		private bool toUpdate = true;

		#endregion

		#region Inherited Properties

		/// <summary>
		/// Gets the field rendering control.
		/// </summary>
		/// <value>The field rendering control.</value>
		public override BaseFieldControl FieldRenderingControl
		{
			[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
			get
			{
				BaseFieldControl fieldControl = new TagFieldControl();
				fieldControl.FieldName = InternalName;

				return fieldControl;
			}
		}

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="TagField"/> class.
		/// </summary>
		/// <param name="fields">The fields.</param>
		/// <param name="fieldName">Name of the field.</param>
		public TagField(SPFieldCollection fields, string fieldName)
			: base(fields, fieldName)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="TagField"/> class.
		/// </summary>
		/// <param name="fields">The fields.</param>
		/// <param name="typeName">Name of the type.</param>
		/// <param name="displayName">The display name.</param>
		public TagField(SPFieldCollection fields, string typeName, string displayName)
			: base(fields, typeName, displayName)
		{
		}

		#endregion

		#region Inherited Methods

		/// <summary>
		/// Called when [added].
		/// </summary>
		/// <param name="op">The op.</param>
		public override void OnAdded(SPAddFieldOptions op)
		{
			base.OnAdded(op);
			SetCustomProperties();
		}

		/// <summary>
		/// Called when [updated].
		/// </summary>
		public override void OnUpdated()
		{
			base.OnUpdated();
			SetCustomProperties();
		}

		#endregion

		#region Auxiliary Methods

		/// <summary>
		/// Sets the custom properties.
		/// </summary>
		private void SetCustomProperties()
		{
			if (toUpdate)
				ConfigureSchemaXml();
			toUpdate = !toUpdate;
		}

		/// <summary>
		/// Configures the schema XML.
		/// </summary>
		private void ConfigureSchemaXml()
		{
			toUpdate = !toUpdate;
			targetName = "Target";
			string target = (string) base.GetCustomProperty(targetName);
			//targetName = AreStringsEqual("New", targetName) ? "New" : "Self";
			XmlDocument doc = new XmlDocument();
			doc.LoadXml(base.SchemaXml);
			if (doc.FirstChild.Attributes[targetName] == null)
			{
				XmlAttribute attrib = doc.CreateAttribute(targetName);
				attrib.Value = target;
				doc.FirstChild.Attributes.Append(attrib);
			}
			else
			{
				doc.FirstChild.Attributes[targetName].Value = target;
			}
			base.SchemaXml = doc.OuterXml;
		}

		#endregion
	}
}